<html>
 
<head>
    <!-- <script type="text/javascript" src="https://cdn.jsdelivr.net/npm/eruda"></script><script>eruda.init();</script> -->
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Nolos五子棋[业余模式]</title>
    <script>
        var black = 2;
        var white = 1;
        var empty = 0;
        var board;
        var context;
        var step = 1;
        var qp = new Array(225);
        var over = false;
        Array.prototype.in_array = function(e) {
            var r = new RegExp(',' + e + ',');
            return (r.test(',' + this.join(this.S) + ','));
        };
        for (var i = 0; i < 225; i++) {
            qp[i] = empty;
        }
        paintqp();
 
        function paintqp() {
            document.write("<center><span id='timer'><font size=20>Nolos五子棋</font></span><br><canvas id=\"cboard\" width=450 height=450></canvas></center><br><span id=rec></span>");
            board = document.getElementById("cboard");
            context = board.getContext("2d");
            for (var i = 0; i < 15; i++) {
                context.moveTo(15 + i * 30, 15);
                context.lineTo(15 + i * 30, 435);
                context.stroke();
                context.moveTo(15, 15 + i * 30);
                context.lineTo(435, 15 + i * 30);
                context.fillStyle = "#F5DEB3";
                context.fillRect(0, 0, 450, 450);
                context.stroke();
            }
            board.onclick = function(e) {
                if (over) return;
                var i = Math.floor(e.offsetX / 30);
                var j = Math.floor(e.offsetY / 30);
                act(i + j * 15);
            };
            setv(112, black);
            /*setv(113,black);
            setv(114,black);
            setv(115,white);
            setv(2,white);
            setv(3,white);
            setv(4,white);
            setv(5,black);*/
        }
 
        function getv2(x, y) {
            if (x < 0 | x > 14 | y < 0 | y > 14) return "blocked";
            return qp[x + y * 15];
        }
 
        function getv(ids) {
            return qp[ids];
        }
 
        function setv(ids, chess) {
            qp[ids] = chess
            j = Math.floor(ids / 15);
            i = ids % 15;
            context.beginPath();
            context.arc(15 + i * 30, 15 + j * 30, 13, 0, 2 * Math.PI);
            if (chess == black) {
                context.fillStyle = "black";
            } else {
                context.fillStyle = "white";
            }
            context.fill();
        }
 
        function iswin(chess) {
            for (var i = 0; i < 225; i++) {
                try {
                    var wwin = true;
                    var bwin = true;
                    for (var j = 0; j < 5; j++) {
                        if (getv(i + j) != chess) {
                            bwin = false;
                        }
                        if ((i + j) % 15 == 14) {
                            if (j < 4) {
                                bwin = false;
                            }
                            break;
                        }
                    }
                    if (bwin == true) {
                        return true;
                    }
                    bwin = true;
                    for (var j = 0; j < 5; j++) {
                        if (getv(i + j * 15) != chess) {
                            bwin = false;
                        }
                    }
                    if (bwin == true) {
                        return true;
                    }
                    bwin = true;
                    for (var j = 0; j < 5; j++) {
                        if (getv(i + j * 15 + j) != chess) {
                            bwin = false;
                        }
                        if ((i + j) % 15 == 14) {
                            if (j < 4) {
                                bwin = false;
                            }
                            break;
                        }
                    }
                    if (bwin == true) {
                        return true;
                    }
                    bwin = true;
                    for (var j = 0; j < 5; j++) {
                        if (getv(i + j * 15 - j) != chess) {
                            bwin = false;
                        }
                        if ((i - j) % 15 == 0) {
                            if (j < 4) {
                                bwin = false;
                            }
                            break;
                        }
                    }
                    if (bwin == true) {
                        return true;
                    }
                } catch (err) {}
            }
        }
 
        function act(index) {
            if (getv(index) == empty) {
                var d1 = (new Date()).getTime();
                setv(index, white);
                document.getElementById("rec").innerHTML += " " + index;
                if (iswin(white) == true) {
                    document.getElementById("timer").innerHTML = "白棋胜！请重新启动程序。";
                    over = true;
                    return;
                }
                ai();
                if (iswin(black) == true) {
                    document.getElementById("timer").innerHTML = "黑棋胜！请重新启动程序。";
                    over = true;
                    return;
                }
                if (step >= 113) {
                    document.getElementById("timer").innerHTML = "平局！请重新启动程序。";
                    over = true;
                    return;
                }
                var d2 = (new Date()).getTime();
                document.getElementById("timer").innerHTML = "本次电脑思考用时" + (d2 - d1).toString() + "毫秒。";
            }
        }
 
        function ai() {
            try {
                var best = [0, -99999];
                var scores = [];
                var v = 0;
                for (var j = 0; j < 1; j++) {
                    for (var i = 0; i < 225; i++) {
                        v = -1;
                        if (getv(i) == empty) {
                            //v=eval(i,1,0.9);
                            if (step == 1 && [96, 98, 126, 128].in_array(i)) {
                                v = eval(i, 1, 0.9) + 15;
                            } else if (step == 2 /*&& [81,83,95,99,125,129,141,143].in_array(i)*/ ) {
                                v = eval(i, 1, 0.9);
                            } else {
                                v = eval(i, 1, 0.68);
                            }
                            if (v > best[1]) {
                                best = [i, v];
                            }
                        }
                        scores[i] = v;
                    }
                }
                //console.log(scores);
                step++;
                setv(best[0], black);
            } catch (err) {
                alert(err);
            }
        }
        //打分函数分隔符
        function eval(point, blackpercent, whitepercent) {
            var score = blackpercent * (chkpt(point, black, [0, 1]) + chkpt(point, black, [1, 0]) + chkpt(point, black, [1, 1]) + chkpt(point, black, [-1, 1])) + whitepercent * (chkpt(point, white, [0, 1]) + chkpt(point, white, [1, 0]) + chkpt(point, white, [1, 1]) + chkpt(point, white, [-1, 1]));
            return score;
        }
 
        function chkpt(pos, player, direction) {
            var x = pos % 15;
            var y = parseInt(pos / 15);
            var count = 0;
            var i;
            var j;
            var vals = [];
            for (i = 1; i < 5; i++) {
                vals[i] = getv2(x + i * direction[0], y + i * direction[1]);
                if (vals[i] == player) {
                    count += 5;
                    if (i >= 2 && vals[i - 1] == empty) {
                        count -= 2.6;
                        if (i >= 3 && vals[i - 2] == empty) count -= 4;
                    }
                } else if (vals[i] == empty) {
                    //count-=2;
                } else {
                    count -= 2.6;
                    if (i >= 2 && vals[i - 1] == empty) count += 0.5;
                    break;
                }
            }
            vals = [];
            for (j = 1; j < 5; j++) {
                vals[j] = getv2(x - j * direction[0], y - j * direction[1]);
                if (vals[j] == player) {
                    count += 5;
                    if (j >= 2 && vals[j - 1] == empty) {
                        count -= 2.6;
                        if (j >= 3 && vals[j - 2] == empty) count -= 4;
                    }
                } else if (vals[j] == empty) {
                    //count-=2;
                } else {
                    count -= 2.6;
                    if (j >= 2 && vals[j - 1] == empty) count += 0.5;
                    break;
                }
            }
            if (i + j < 6) return 0;
            //if(count>=10.5) count+=9;
            //冲四提分
            //if(count>=8.5) count+=5;
            //活三提分
            return Math.pow(1.35, Math.max(count, 0));
        }
    </script>
</head>

</html>
